package com.alliky.core.net;

import com.alliky.core.api.BaseApi;
import com.alliky.core.entity.BaseResponse;
import com.alliky.core.net.upload.FileUploadObserver;
import com.alliky.core.net.upload.UploadFileRequestBody;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import retrofit2.http.PartMap;
import retrofit2.http.Url;

/**
 * ================================================
 *
 * @Description: 描述
 * @Author: wxianing
 * @Date: 2022/7/14 10:52
 * ================================================
 */
public class UploadManager {
    private static UploadManager instance;

    private static Retrofit mRetrofit;

    protected ApiService mApiService;

    private UploadManager() {
        buildNetWork();
        mApiService = mRetrofit.create(ApiService.class);
    }

    /**
     * 单例模式
     *
     * @return DownLoadManager
     */
    public static UploadManager getInstance() {
        if (instance == null) {
            instance = new UploadManager();
        }
        return instance;
    }

    //下载
    public void upload(String uploadUrl, File file, FileUploadObserver<BaseResponse<String>> fileUploadObserver) {

        HashMap<String, RequestBody> hashMap = new HashMap();

        //公共参数
        hashMap.put("timestamp", toRequestBody(String.valueOf(System.currentTimeMillis() / 1000)));
        hashMap.put("sys_type", toRequestBody("app"));
        hashMap.put("client_device", toRequestBody("android"));

        UploadFileRequestBody uploadFileRequestBody = new UploadFileRequestBody(file, fileUploadObserver);
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), uploadFileRequestBody);
        mApiService.upload(uploadUrl, part, new HashMap<>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(fileUploadObserver);
    }

    private RequestBody toRequestBody(String value) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), value);
        return requestBody;
    }

    private void buildNetWork() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new ProgressInterceptor())
                .connectTimeout(60, TimeUnit.SECONDS)
                .build();

        mRetrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BaseApi.API_HOST)
                .build();
    }


    private interface ApiService {

        //单文件上传
        @Multipart
        @POST
        Observable<BaseResponse<String>> upload(@Url String url, @Part MultipartBody.Part file, @PartMap HashMap<String, RequestBody> params);

        //多文件上传
        @Multipart
        @POST
        Observable<String> uploads(@Url String url, @Part List<MultipartBody.Part> file);
    }
}
